package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import javax.security.auth.callback.LanguageCallback;
import java.lang.invoke.LambdaMetafactory;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public R add(ShoppingCart shoppingCart) {

        //获取userId,设置到shoppingCart对象中
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //设置查询条件 菜品id或套餐id相同 且 userId相同
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId() != null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        if(shoppingCart.getSetmealId() != null){
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        wrapper.eq(ShoppingCart::getUserId,userId);
        ShoppingCart one = this.getOne(wrapper);

        //若存在,数量加1
        if(one != null){
            one.setNumber(one.getNumber() + 1);
            this.updateById(one);
            return R.success(one);
        }

        //若不存在,加入购物车,数量默认为1
        this.save(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车
     */
    @Override
    public R getAll() {

        //设置查询条件 -- userId  排序条件:根据加入购物车时间降序排列
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = this.list(wrapper);
        return R.success(shoppingCarts);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {

        //设置清空条件 -- userId
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        this.remove(wrapper);
    }

    /**
     * 购物车减一
     * @param shoppingCart
     */
    @Override
    public R sub(ShoppingCart shoppingCart) {

        //获取userId,设置到shoppingCart对象中
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //设置查询条件 菜品id或套餐id相同 且 userId相同
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId() != null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        if(shoppingCart.getSetmealId() != null){
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        wrapper.eq(ShoppingCart::getUserId,userId);
        ShoppingCart one = this.getOne(wrapper);

        //数量为1,直接删除
        if(one.getNumber() == 1){
            this.removeById(one.getId());
            return R.success("操作成功");
        }

        //数量大于1,进行数量和价格的修改
        one.setNumber(one.getNumber() - 1);
        //cart.setAmount(cart.getAmount() - cart.getAmount()/cart.getNumber());
        this.updateById(one);
        return R.success("操作成功");
    }
}